package com.maya.medicalwastes.util;

import android.content.Context;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.provider.Settings;


/**
 * *
 */
public class NFCUtils {
    public static String bytesToHexString(byte[] bArray) {
        StringBuffer strBuf = new StringBuffer(bArray.length);
        String strTemp;
        for (int i = 0; i < bArray.length; i++) {
            strTemp = Integer.toHexString(0xFF & bArray[i]);
            if (strTemp.length() < 2) {
                strBuf.append(0);
            }
            strBuf.append(strTemp.toUpperCase());
        }
        return strBuf.toString();
    }

    public static boolean isCanUseNFC(Context context) {
        NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(context);
        //3.获取nfc适配器  
        if (mNfcAdapter == null) {
            //3.如果获取的mNfcAdapter=null，则说明该手机不支持nfc功能  
            ToastUtils.ViewToast(context, "该设备不支持nfc");
            return false;
        } else {
            //4.如果手机有nfc功能，进一步判断nfc是否打开      
            // 5.假如手机的nfc功能没有被打开。则跳到打开nfc功能的界面      
            if (!mNfcAdapter.isEnabled()) {
                ToastUtils.ViewToast(context, "先打开nfc功能");
                Intent setNfc = new Intent(Settings.ACTION_NFC_SETTINGS);
                context.startActivity(setNfc);
            }
            return true;
        }
    }

    public static String getRfidId(Intent intent, Context context) {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        if (tag != null) {
            //解析卡片信息 获取卡ID
            String tagId = NFCUtils.bytesToHexString(tag.getId());
            return tagId;
        } else {
//            ToastUtils.ViewToast(context, "没有获取到RFID信息");
            return "";
        }
    }
}
